import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:mpc_plugin/mpc_plugin.dart';

class forgingMPCResp {
  String mpcPieceA;
  String mpcPieceB;
  String mpcPieceC;
  forgingMPCResp(this.mpcPieceA, this.mpcPieceB, this.mpcPieceC);
}

class escapeResp {
  String mnemonic;
  String path;
  escapeResp(this.mnemonic, this.path);
}

class mpcTool {
  Future<forgingMPCResp> forgingMPC(String salt, String? path) async {
    try {
      var result = await MpcPlugin().forgingMPC(salt, path ?? '');
      if (result != null) {
        return forgingMPCResp(
            result['mpcPieceA'], result['mpcPieceB'], result['mpcPieceC']);
      }
      return forgingMPCResp('', '', '');
    } catch (e) {
      return forgingMPCResp('', '', '');
    }
  }

  Future<forgingMPCResp> useMnemonicForgingMPC(
      String mne, String salt, String? path) async {
    try {
      var result =
          await MpcPlugin().useMnemonicForgingMPC(mne, salt, path ?? '');
      if (result != null) {
        return forgingMPCResp(
            result['mpcPieceA'], result['mpcPieceB'], result['mpcPieceC']);
      }
      return forgingMPCResp('', '', '');
    } catch (e) {
      return forgingMPCResp('', '', '');
    }
  }

  Future<int> verifyMPCFragment(String piece) async {
    try {
      var result = await MpcPlugin().verifyMPCFragment(piece);
      if (result != null) {
        return result['result'];
      }
      return -1;
    } catch (e) {
      kaiPrint(e);
      return -1;
    }
  }

  Future<String> recoverMPCFragment(String piece1, String piece2) async {
    try {
      var result = await MpcPlugin().recoverMPCFragment(piece1, piece2);
      if (result != null) {
        return result['result'];
      }
      return '';
    } catch (e) {
      kaiPrint(e);
      return '';
    }
  }

  Future<String> mpcFragmentGetPublicKey(String piece) async {
    try {
      var result = await MpcPlugin().mpcFragmentGetPublicKey(piece);
      if (result != null) {
        return result['result'];
      }
      return '';
    } catch (e) {
      kaiPrint(e);
      return '';
    }
  }

  Future<escapeResp> mpcEscape(
      String piece1, String piece2, String salt) async {
    try {
      var result = await MpcPlugin().mpcEscape(piece1, piece2, salt);
      kaiPrint('mpcEscape result:  $result');
      if (result != null) {
        return escapeResp(result['mnemonic']??'', result['path']??'');
      }
      return escapeResp('', '');
    } catch (e) {
      kaiPrint('mpcEscape $e');
      return escapeResp('', '');
    }
  }

  Future<String> signTx(
      String txSigner, String piece1, String piece2, String salt) async {
    try {
      var result = await MpcPlugin().signTx(txSigner, piece1, piece2, salt);
      if (result != null) {
        return result['result'];
      }
      return '';
    } catch (e) {
      kaiPrint(e);
      return '';
    }
  }

  Future<String> solSignTx(
      String txSigner, String piece1, String piece2, String salt) async {
    try {
      var result = await MpcPlugin().solSignTx(txSigner, piece1, piece2, salt);
      if (result != null) {
        return result['result'];
      }
      return '';
    } catch (e) {
      kaiPrint('solSignTx::::: $e');
      return '';
    }
  }

  Future<int> getVersionFromFrag(String frag) async {
    try {
      var result = await MpcPlugin().getVersionFromFrag(frag);
      kaiPrint('getVersionFromFrag result: $result');
      if (result != null) {
        return result;
      }
      return 0;
    } catch (e) {
      kaiPrint(e);
      return 0;
    }
  }


  Future<String> getAddress(String frag,String chainId) async {
    try {
      var result = await MpcPlugin().getAddress(frag,chainId);
      if (result != null) {
        return result['address']??'';
      }
      return '';
    } catch (e) {
      kaiPrint(e);
      return '';
    }
  }

  Future<forgingMPCResp> upgradeMPC(String frag1,String frag2,String salt) async {
    try {
      var result = await MpcPlugin().upgradeMPC(frag1,frag2, salt);
      if (result != null) {
        return forgingMPCResp(
            result['mpcPieceA'], result['mpcPieceB'], result['mpcPieceC']);
      }
      return forgingMPCResp('', '', '');
    } catch (e) {
      return forgingMPCResp('', '', '');
    }
  }

  //转公钥地址
  // Future<String> generateAddress() {}
}
